package com.blog.common;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @create 2018-06-06 16:35
 **/
public class ServerResponse  {
    // 状态码 000000：成功；999999：失败
    private String code;
    // 错误信息
    private String message;
    // 返回的数据（链式）
    private Map<String, Object> data = new HashMap<String, Object>();

    public static ServerResponse success() {
        ServerResponse result = new ServerResponse();
        result.setCode("000000");
        result.setMessage("操作成功");
        return result;
    }

    public static ServerResponse error(String msg) {
        ServerResponse result = new ServerResponse();
        result.setCode("999999");
        if (StringUtils.isEmpty(msg)) {
            result.setMessage("操作失败");
        } else {
            result.setMessage(msg);
        }
        return result;
    }
    //返回错误信息的状态码
    public static ServerResponse error() {
        ServerResponse result = new ServerResponse();
        result.setCode("999999");
        return result;
    }

    // 链式操作返回信息
    public ServerResponse add(String key, Object value) {
        this.getData().put(key, value);
        return this;
    }

    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Map<String, Object> getData() {
        return data;
    }

    public void setData(Map<String, Object> data) {
        this.data = data;
    }
}
